package com.globallogic.vkulyk.SSHTool;

import java.util.ArrayList;

import org.junit.runner.RunWith;
import org.junit.*;
import org.powermock.modules.junit4.PowerMockRunner;
import org.powermock.core.classloader.annotations.*;
import org.powermock.api.mockito.PowerMockito;
import org.mockito.*;
import org.mockito.Mock;

@RunWith(PowerMockRunner.class)
@PrepareForTest({ CLITargetModeHandler.class })
public class Unit_CLITargetModeHandler extends SSHToolTester {
	@Mock
	XMLHandler mockedXMLHandler;
	@Mock
	SocketsAndActionsDataKeeper mockedSocketsAndActionsData;
	@Mock
	ArrayList<ActionDataKeeper> mockedActionsData;
	@Mock
	ArrayList<SocketDataKeeper> mockedSocketsData;
	@Mock
	ActionsOnSocketsExecutor mockedActionsOnSocketsExecutor;

	@Before
	public void setup() throws Exception {
		// given
		setVarsFromPropertiesFile("testData/testing.properties");
		PowerMockito.whenNew(XMLHandler.class)
				.withArguments(Mockito.anyString(), Mockito.anyString())
				.thenReturn(mockedXMLHandler);
		Mockito.when(mockedXMLHandler.parseXML()).thenReturn(
				mockedSocketsAndActionsData);
		Mockito.when(mockedSocketsAndActionsData.getSockets()).thenReturn(
				mockedSocketsData);
		Mockito.when(mockedSocketsAndActionsData.getActions()).thenReturn(
				mockedActionsData);
		PowerMockito
				.whenNew(ActionsOnSocketsExecutor.class)
				.withArguments(Mockito.any(ArrayList.class),
						Mockito.any(ArrayList.class))
				.thenReturn(mockedActionsOnSocketsExecutor);
		String[] args = { "-f", targetsFile, "-t", target };

		// when
		new CLITargetModeHandler(args).run();
	}

	@Test
	public void shouldInstantiateXMLHandlerWithCorrectArgsAndCallParseXML_whenCalledRun()
			throws Exception {
		// then
		PowerMockito.verifyNew(XMLHandler.class).withArguments(
				Mockito.eq(targetsFile), Mockito.eq(target));
		Mockito.verify(mockedXMLHandler).parseXML();
	}

	@Test
	public void shouldInstantiateExecutorWithCorrectArgsAndCallExecute_whenCalledRun()
			throws Exception {
		// then
		PowerMockito.verifyNew(ActionsOnSocketsExecutor.class).withArguments(
				Mockito.eq(mockedSocketsData), Mockito.eq(mockedActionsData));
		Mockito.verify(mockedActionsOnSocketsExecutor).execute();
	}
}
